﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NetworkClient : MonoBehaviour {
    public Text msgid, msgcontent, recvcontent;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        byte[] recvBuffer = TCPClient.Instance.RecvMsg();
        if(recvBuffer != null)
        {
            SimpleGameMsg msg = ProtobufTool.Deserialize<SimpleGameMsg>(recvBuffer);
            recvcontent.text = msg.msgType.ToString() + "\n" + msg.msgContent;
        }
	}

    public void Connect()
    {
        TCPClient.Instance.Connect();
    }

    public void Disconnect()
    {
        TCPClient.Instance.DisConnect();
    }

    public void Send()
    {
        SimpleGameMsg msg = new SimpleGameMsg();
        msg.msgType = int.Parse(msgid.text);
        msg.msgContent = msgcontent.text;
        TCPClient.Instance.SendMsg(ProtobufTool.Serialize(msg));
    }

    public void OnDestroy()
    {
        TCPClient.Instance.Close();
    }
}
